package com.simon.recyclerview;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.simon.recyclerview.adapter.TestAdapter;
import com.simon.recyclerview.utils.ToastHelper;
import com.simon.recyclerview.utils.ToastUtils;
import com.simon.refreshlibrary.recyclerview.adapter.UniversalAdapter;
import com.simon.refreshlibrary.recyclerview.listeners.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

public class BaseActivity extends AppCompatActivity implements UniversalAdapter.LoadMoreListener {

    private static final int TOTAL_COUNTER = 45;
    private int delayMillis = 2000;

    private int mCurrentCounter = 0;
    private boolean isErr;
    private View notLoadingView;

    private TestAdapter mAdapter;
    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        String[] array = getResources().getStringArray(R.array.test_data);
        List<String> sampleData = getSampleData(15);
        mAdapter = new TestAdapter(sampleData);
        mCurrentCounter = mAdapter.getData().size();
        mAdapter.setLoadMoreEnable(true);
        mAdapter.openAnimation();
        mAdapter.setFirstOnlyAnim(false);

        View headerView = getView(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.addHeaderView(getView(getRemoveHeaderListener(), "click me to remove " +
                        "me"), 0);
            }
        }, "click me to add new header");
        mAdapter.addHeaderView(headerView);

        View footerView = getView(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.addFooterView(getView(getRemoveFooterListener(), "click me to remove me"));
            }
        }, "click me to add new footer");
        mAdapter.addFooterView(footerView, 0);
        mAdapter.setOnLoadMoreListener(this);


        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(UniversalAdapter adapter, RecyclerView recyclerView, View
                    view, int position) {
//                ToastHelper.shortToast("" + position);
                ToastUtils.showLongToast(App.context(), "" + position);
            }
        });

        mRecyclerView.setAdapter(mAdapter);
    }


    private View getView(View.OnClickListener listener, String text) {
        View view = getLayoutInflater().inflate(R.layout.head_view, null);
        view.setLayoutParams(new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        ((TextView) view.findViewById(R.id.tv)).setText(text);
        view.setOnClickListener(listener);
        return view;
    }

    private View.OnClickListener getRemoveHeaderListener() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.removeHeaderView(v);
            }
        };
    }

    private View.OnClickListener getRemoveFooterListener() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.removeFooterView(v);
            }
        };
    }

    @Override
    public void onLoadMore() {
        mRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                if (mCurrentCounter >= TOTAL_COUNTER) {

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (notLoadingView == null) {
                                notLoadingView = getLayoutInflater().inflate(R.layout.not_loading,
                                        (ViewGroup) mRecyclerView.getParent(), false);
                            }
                            mAdapter.showNOMoreView();
                        }
                    }, delayMillis);

                } else {
                    if (isErr) {
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mAdapter.loadComplete();
                                mAdapter.appendData(getSampleData(15));
                                mCurrentCounter = mAdapter.getData().size();
                            }
                        }, delayMillis);
                    } else {
                        isErr = true;
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                ToastHelper.longToast("请求失败");
                                mAdapter.showLoadMoreFailedView();
                            }
                        }, delayMillis);


                    }
                }
            }

        });
    }

    public static List<String> getSampleData(int lenth) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < lenth; i++) {

            list.add("RecyclerView Item " + i);
        }
        return list;
    }

}
